#!/bin/bash
admin_properties=/data/tomcat/webapps/djcloudadmin/WEB-INF/classes/application.properties
api_properties=/data/tomcat/webapps/djcloudapi/WEB-INF/classes/application.properties

# SET JAVA_OPTS
if [[ ${JAVA_OPTS} ]]; then
    sed -i "5a JAVA_OPTS=\"${JAVA_OPTS}\"" /data/tomcat/bin/catalina.sh
fi

# SET MYSQL_URL
# Example:jdbc:mysql://10.45.40.135:3306/djcloud_show_20190125?characterEncoding=utf8&useSSL=true
if [[ ${MYSQL_URL} ]]; then
    sed -i '/^spring.datasource.druid.url=/d' ${admin_properties} ${api_properties} ${web_properties}
    echo -e "\nspring.datasource.druid.url=${MYSQL_URL}" |tee -a ${admin_properties} ${api_properties} ${web_properties}
fi

# SET MYSQL_USER
# Example:root
if [[ ${MYSQL_USER} ]]; then
    sed -i '/^spring.datasource.druid.username=/d' ${admin_properties} ${api_properties} ${web_properties}
    echo -e "\nspring.datasource.druid.username=${MYSQL_USER}" |tee -a ${admin_properties} ${api_properties} ${web_properties}
fi

# SET MYSQL_PASSWD
# Example:aaa123456
if [[ ${MYSQL_PASSWD} ]]; then
    sed -i '/^spring.datasource.druid.password=/d' ${admin_properties} ${api_properties} ${web_properties}
    echo -e "\nspring.datasource.druid.password=${MYSQL_PASSWD}" |tee -a ${admin_properties} ${api_properties} ${web_properties}
fi

cd /data/tomcat/bin && ./catalina.sh run
exec "$@"docker/docker-entrypoint.sh